#include "serial.h"
#include "ui_serial.h"
#include <QtGui>

serial::serial(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::serial)
{
    ui->setupUi(this);
    dnw = new QProcess(this);
    dnwfile = "dnw";
    ui->sendMsgBtn->setEnabled(false); //开始“发送数据”按钮不可用
    ui->sendMsgLineEdit->setEnabled(false);
    QColor c(0,255,0,255);
    //ui->textBrowser->setTextColor(c);
    connect(ui->sendMsgLineEdit, SIGNAL(returnPressed()), this, SLOT(on_sendMsgBtn_clicked()));
    ui->textBrowser->setText(tr("要使用，请打开串口..."));
    connect(ui->openSerialAction, SIGNAL(triggered()), this, SLOT(openSerial()));
    connect(ui->closeSerialAction, SIGNAL(triggered()), this, SLOT(closeSerial()));
    connect(ui->USBDownAction, SIGNAL(triggered()), this, SLOT(USBDown()));
    connect(ui->aboutAction, SIGNAL(triggered()), this, SLOT(About()));
    this->setWindowIcon(QPixmap(":/env.png"));
}

serial::~serial()
{
    delete ui;
}

void serial::changeEvent(QEvent *e)
{
    QMainWindow::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void serial::getStr(QByteArray str)
{
    if(str.length()>=2)
    {
    str.replace("[1;34m", "");
    str.replace("[0m", "");
    str.replace("[1;36m", "");
    str.replace("[1;32m", "");
    str.replace("[34m", "");
    str.replace("    ", " ");
    str.replace("   ", " ");
    str.replace("  ", " ");
    str.replace(" ", "   ");
    ui->textBrowser->textCursor().insertText(str);
    ui->textBrowser->moveCursor(QTextCursor::End,QTextCursor::KeepAnchor);
    }
}

void serial::openSerial()
{
    /*if(ui->portNameComboBox->currentText() == tr("ttyS0"))
        portName = "/dev/ttyS0";
    else if(ui->portNameComboBox->currentText() == tr("ttyS1"))
        portName = "/dev/ttyS1";
    else if(ui->portNameComboBox->currentText() == tr("ttyUSB0"))
        portName = "/dev/ttyUSB0";*/
    QString ttyName("/dev/" + ui->portNameComboBox->currentText());
    portName = ttyName;
    qDebug() << portName;

    if(ui->baudRateComboBox->currentText() == tr("9600"))
        mybaud = BAUD9600;
    else if(ui->baudRateComboBox->currentText() == tr("115200"))
        mybaud = BAUD115200;

    if(ui->dataBitsComboBox->currentText() == tr("8"))
            mydatabits = DATA_8;
    else if(ui->dataBitsComboBox->currentText() == tr("7"))
            mydatabits = DATA_7;

    if(ui->parityComboBox->currentText() == tr("无"))
            myparity = PAR_NONE;
    else if(ui->parityComboBox->currentText() == tr("奇"))
            myparity = PAR_ODD;
    else if(ui->parityComboBox->currentText() == tr("偶"))
           myparity = PAR_EVEN;

    if(ui->stopBitsComboBox->currentText() == tr("1"))
            mystopbits = STOP_1;
    else if(ui->stopBitsComboBox->currentText() == tr("2"))
            mystopbits = STOP_2;

    if(ui->flowCombox->currentText() == tr("无"))
        myflow = FLOW_OFF;
    else if(ui->flowCombox->currentText() == tr("硬件"))
        myflow = FLOW_HARDWARE;
    else if(ui->flowCombox->currentText() == tr("FLOW_XONXOFF"))
        myflow = FLOW_XONXOFF;

    mytimeout = (long)ui->timeOutBox->value();

    myserial = new SerialThread(portName, mybaud, mydatabits, myparity, mystopbits, myflow, mytimeout);
    connect(myserial , SIGNAL(serialFinished(QByteArray)), this, SLOT(getStr(QByteArray)));

    ui->sendMsgLineEdit->setFocus();
    ui->sendMsgBtn->setEnabled(true);
    ui->sendMsgLineEdit->setEnabled(true);
    ui->baudRateComboBox->setEnabled(false); //设置各个组合框不可用
    ui->dataBitsComboBox->setEnabled(false);
    ui->parityComboBox->setEnabled(false);
    ui->stopBitsComboBox->setEnabled(false);
    ui->portNameComboBox->setEnabled(false);
    ui->flowCombox->setEnabled(false);
    ui->portNameComboBox->setEnabled(false);
    ui->sendMsgBtn->setDefault(true);
    ui->timeOutBox->setEnabled(false);
    myserial->start();
    ui->textBrowser->setText(tr("串口 ") + ui->portNameComboBox->currentText() + tr(" 已打开"));
}



void serial::closeSerial()
{
    myserial->myCom->close();                         //关闭串口，该函数在win_qextserialport.cpp文件中定义
    ui->sendMsgBtn->setEnabled(false);      //关闭串口后“发送数据”按钮不可用

    ui->baudRateComboBox->setEnabled(true); //设置各个组合框可用
    ui->dataBitsComboBox->setEnabled(true);
    ui->parityComboBox->setEnabled(true);
    ui->stopBitsComboBox->setEnabled(true);
    ui->portNameComboBox->setEnabled(true);
    ui->flowCombox->setEnabled(true);
    ui->timeOutBox->setEnabled(true);
    ui->sendMsgLineEdit->setEnabled(false);
    ui->textBrowser->clear();
    ui->textBrowser->setText(tr("串口 ") + ui->portNameComboBox->currentText() + tr(" 已关闭"));
}

void serial::on_sendMsgBtn_clicked()
{
    if(ui->sendMsgLineEdit->text().isEmpty()){
        myserial->myCom->putChar('\n');
    } else {
        myserial->myCom->write(ui->sendMsgLineEdit->text().toAscii());       //以ASCII码形式将行编辑框中的数据写入串口
        myserial->myCom->write("\n");
        ui->sendMsgLineEdit->clear();
    }
}

void serial::USBDown()
{
  QString fileName = QFileDialog::getOpenFileName(this, tr("open file"), " ", tr("allFiles (*.*)"));
  if(!fileName.isEmpty())
  {
      args << fileName;
      dnw->start(dnwfile, args);
      args.clear();
  } else
      return;
}

void serial::About()
{
   QDialog *dialog = new QDialog(this);
   dialog->resize(200,120);
   QLabel *label01 = new QLabel(tr("\n利用的是第三方的串口类！"),dialog);
   QLabel *label02 = new QLabel(tr("                 赵文杰"),dialog);
   QLabel *label03 = new QLabel(tr("                 2010/05/20"),dialog);
   QVBoxLayout *layout = new QVBoxLayout;
   layout->addWidget(label01);
   layout->addWidget(label02);
   layout->addWidget(label03);
   dialog->setLayout(layout);
   dialog->show();

}
